package cn.wolfcode.web.controller;


import cn.wolfcode.domain.Activity;
import cn.wolfcode.domain.ActivityItem;
import cn.wolfcode.domain.Choice;
import cn.wolfcode.domain.Employee;
import cn.wolfcode.qo.JsonResult;
import cn.wolfcode.service.IActivityItemService;
import cn.wolfcode.service.IActivityService;
import cn.wolfcode.qo.QueryObject;
import cn.wolfcode.util.RequiredPermission;
import cn.wolfcode.util.UserContext;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/activity")
public class ActivityController {

    @Autowired
    private IActivityService activityService;

    @Autowired
    private IActivityItemService activityItemService;

    @RequiredPermission(name="投票主题页面",expermission="activity:list")
    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo") QueryObject qo){
        PageInfo<Activity> pageInfo = activityService.query(qo);
        model.addAttribute("pageInfo", pageInfo);
        return "activity/list";
    }


    @RequiredPermission(name="投票主题删除",expermission="activity:delete")
    @RequestMapping("/delete")
    public String delete(Long id) {
        if (id != null) {
           activityService.delete(id);
        }
        return "redirect:/activity/list";
    }


    @RequiredPermission(name="投票主题新增/编辑",expermission="activity:saveOrUpdate")
    @RequestMapping("/saveOrUpdate")
    public String saveOrUpdate(Activity activity) {
        if (activity.getId() == null) {
            activityService.save(activity);
        } else {
            activityService.update(activity);
        }
        return "redirect:/activity/list";
    }


    @RequiredPermission(name="投票主题新增/编辑",expermission="activity:saveOrUpdate")
    @RequestMapping("/input")
    public String input(Long id, Model model) {
        if (id != null) {
            model.addAttribute("activity",activityService.get(id));
            model.addAttribute("items",activityService.selectByAid(id));
        }
        return "activity/input";
    }


    @RequestMapping("/show")
    @RequiredPermission(name = "投票活动提交",expermission = "activity:show")
    @ResponseBody
    public String show(Long id,Model model){
        ActivityItem item = activityItemService.get(id);
        Activity activity = activityService.get(item.getActivityId());
        model.addAttribute("activity",activity);
        List<ActivityItem> activityItems = activityItemService.selectItemById(item.getActivityId());
        model.addAttribute("result",activityItems);
        int count=0;
        for (ActivityItem activityItem : activityItems) {
            count+=activityItem.getNum();
        }
        activity.setTotalNum(count);
        model.addAttribute("count",count);
        return "activity/show";
    }

    @RequestMapping("/submit")
    @ResponseBody
    public JsonResult submit(Long ActivityId,Long ActivityItemId){
        Employee emp = UserContext.getCurrentUser();
        Long empId = emp.getId();
        Choice choice = activityService.selectByempIdAndAid(empId, ActivityId);
    if (choice==null){
        activityService.insertChoice(empId,ActivityItemId);
        ActivityItem activityItem = activityService.getActivityItem(ActivityItemId);
        activityItem.setNum(activityItem.getNum()+1);
        activityService.updateActivityItem(activityItem);
        return new JsonResult(true,"投票成功");
    }else {
        return new JsonResult(false,"您已经投过票了");
    }
      }

}
